<template>
    <div class="register">
        <h3>
            注册新用户
            <span class="go"
                >我有账号，去 <router-link to="/">登录</router-link>
            </span>
        </h3>
        <div class="content">
            <label>手机号:</label>
            <input
                v-model="formData.phone"
                placeholder="请输入你的手机号"
                name="phone"
            />
        </div>
        <div class="content">
            <label>验证码:</label>
            <input
                type="text"
                placeholder="请输入验证码"
                v-model="formData.code"
                name="code"
            />
            <button
                @click="getPhoneCode"
                style="height: 38px; padding: 0 5px"
                :disabled="second < 5"
            >
                {{ second < 5 ? `${second}秒之后可重新发送` : "获取验证码" }}
            </button>
        </div>
        <div class="content">
            <label>登录密码:</label>
            <input
                type="text"
                placeholder="请输入你的登录密码"
                v-model="formData.password"
                name="password"
            />
        </div>
        <div class="content">
            <label>确认密码:</label>
            <input
                type="text"
                placeholder="请输入确认密码"
                v-model="formData.repassword"
                name="repassword"
            />
        </div>
        <div class="controls">
            <input
                type="checkbox"
                v-model="formData.isAgreen"
                name="isAgreen"
            />
            <span>同意协议并注册《尚品汇用户协议》</span>
        </div>
        <div class="btn">
            <button @click="register">完成注册</button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import Pubsub from 'pubsub-js'
import { useRouter } from "vue-router";
import loginApi ,{type registerModel}from "@/api/login";
import { ElMessage } from "element-plus";
const router = useRouter();
//  收集用户填写数据,发送请求,注册账号
const formData = ref<registerModel>({
    phone: "", // 手机号
    code: "", // 验证码
    password: "", // 密码
    repassword: "", // 确认密码
    isAgreen: true, // 是否统一协议
});
//验证码的计时器 防止过多发送请求
const timer = ref();
//倒计时时间
const second = ref(5);
// 注册逻辑
const register = async () => {
    // 简单校验
    let { phone, code, password, repassword, isAgreen } = formData.value;
    if (!(phone && code && password && password == repassword && isAgreen)) {
        alert("请输入合法内容");
        return;
    }
    try {
        await loginApi.reqRegister(formData.value);
        ElMessage.success('注册成功,请点击登录')
        Pubsub.publish('LOGIN','ok')
        router.push("/"); // 注册成功之后,跳转首页 资源
    } catch (error) {
        console.error(error);
    }
};
// 获取验证码
const getPhoneCode = async () => {
    if (!formData.value.phone) {
        alert("请输入手机号");
        return;
    }
    // 倒计时
    timer.value = setInterval(() => {
        if (second.value == 0) {
            clearInterval(timer.value);
            second.value = 5; // 重置时间
            return;
        }
        second.value--;
    }, 1000);
    // 发请求获取验证码
    try {
        formData.value.code = (await loginApi.reqCode(formData.value.phone) as any);
    } catch (error) {
        console.error(error);
    }
};
</script>

<style lang="less" scoped>
.register {
    width: 1200px;
    height: 445px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;

    h3 {
        background: #ececec;
        margin: 0;
        padding: 6px 15px;
        color: #333;
        border-bottom: 1px solid #dfdfdf;
        font-size: 20.04px;
        line-height: 30.06px;

        span {
            font-size: 14px;
            float: right;

            a {
                color: #e1251b;
            }
        }
    }

    div:nth-of-type(1) {
        margin-top: 40px;
    }

    .content {
        padding-left: 390px;
        margin-bottom: 18px;
        position: relative;

        label {
            font-size: 14px;
            width: 96px;
            text-align: right;
            display: inline-block;
        }

        input {
            width: 270px;
            height: 38px;
            padding-left: 8px;
            box-sizing: border-box;
            margin-left: 5px;
            outline: none;
            border: 1px solid #999;
        }

        img {
            vertical-align: sub;
        }

        .error-msg {
            position: absolute;
            top: 100%;
            left: 495px;
            color: red;
        }
    }

    .controls {
        text-align: center;
        position: relative;

        input {
            vertical-align: middle;
        }

        .error-msg {
            position: absolute;
            top: 100%;
            left: 495px;
            color: red;
        }
    }

    .btn {
        text-align: center;
        line-height: 36px;
        margin: 17px 0 0 55px;

        button {
            outline: none;
            width: 270px;
            height: 36px;
            background: #e1251b;
            color: #fff !important;
            display: inline-block;
            font-size: 16px;
        }
    }
}
</style>
